﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.ServiceModel;
using System.Web;
using DHQGHN.DAL.BO.DanhMuc;

namespace DHQGHN.Services.DanhMuc
{
    public class AdminUnitExtraService: IAdminUnitExtraService
    {
        log4net.ILog logger = log4net.LogManager.GetLogger(typeof(AdminUnitExtraService));
        private AdministrativeUnitExtBO adminUnitExtBO;
        #region Constructors
        public AdminUnitExtraService()
        {
            //string connString = ConfigurationManager.ConnectionStrings["DHQGHN_TDT.ConnectionString"].ConnectionString;
            adminUnitExtBO = new AdministrativeUnitExtBO(DHQGHN.DAL.Common.ConnectionUtil.TKTHDS_TDT_CONNECTION_STRING);
        }
        #endregion


        public DAL.Models.DanhMuc.ADM_UNIT_EXT GetByID(string adminUnitExtraID)
        {
            try
            {
                string id = "-1";

                if (string.IsNullOrEmpty(adminUnitExtraID))
                {
                    Fault.ValidationFault fault = new Fault.ValidationFault()
                    {
                        Result = false,
                        Message = "Thiếu tham số",
                        Description = "Thiếu tham số"
                    };

                    throw new FaultException<Fault.ValidationFault>(fault);
                }
                id = adminUnitExtraID;
                //if (long.TryParse(adminUnitExtraID, out id) == false)
                //{
                //    Fault.ValidationFault fault = new Fault.ValidationFault()
                //    {
                //        Result = false,
                //        Message = "Tham số areaID sai định dạng!",
                //        Description = "Tham số areaID sai định dạng!"
                //    };

                //    throw new FaultException<Fault.ValidationFault>(fault);
                //}

                return adminUnitExtBO.GetById(id);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }

        public IEnumerable<DAL.Models.DanhMuc.ADM_UNIT_EXT> GetByAdminCodeAndType(string adminCode, string strType)
        {
            try
            {
                long id = -1;

                if (string.IsNullOrEmpty(adminCode) || string.IsNullOrEmpty(strType))
                {
                    Fault.ValidationFault fault = new Fault.ValidationFault()
                    {
                        Result = false,
                        Message = "Thiếu tham số",
                        Description = "Thiếu tham số"
                    };

                    throw new FaultException<Fault.ValidationFault>(fault);
                }

                if (long.TryParse(adminCode, out id) == false)
                {
                    Fault.ValidationFault fault = new Fault.ValidationFault()
                    {
                        Result = false,
                        Message = "Tham số areaID sai định dạng!",
                        Description = "Tham số areaID sai định dạng!"
                    };

                    throw new FaultException<Fault.ValidationFault>(fault);
                }

                return adminUnitExtBO.GetByAdminCodeAndType(adminCode, strType);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }

        /// <summary>
        /// phunx1, 20150513: luu thong tin vao bang Administ_Unit_Extra
        /// </summary>
        /// <param name="adExt"></param>
        public void AddAdminUnitExt(DAL.Models.DanhMuc.ADM_UNIT_EXT adExt)
        {
            try
            {
                adminUnitExtBO.Save(adExt);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }

        public IEnumerable<DAL.Models.DanhMuc.ADM_UNIT_EXT> GetAdminExtDropDown(string adminCode, string parentID, string strType)
        {
            try
            {
                string id = "-1";

                if (string.IsNullOrEmpty(adminCode) || string.IsNullOrEmpty(strType))
                {
                    Fault.ValidationFault fault = new Fault.ValidationFault()
                    {
                        Result = false,
                        Message = "Thiếu tham số",
                        Description = "Thiếu tham số"
                    };

                    throw new FaultException<Fault.ValidationFault>(fault);
                }

                //if (long.TryParse(areaID, out id) == false)
                //{
                //    Fault.ValidationFault fault = new Fault.ValidationFault()
                //    {
                //        Result = false,
                //        Message = "Tham số areaID sai định dạng!",
                //        Description = "Tham số areaID sai định dạng!"
                //    };

                //    throw new FaultException<Fault.ValidationFault>(fault);
                //}
                return adminUnitExtBO.GetAdminExtDropDown(adminCode, parentID, strType);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }
    }
}